<?php
/**
 * 캐쉬 관리 - File
 *
 * @package util
 * @subpackage cache
 *
 * @author jylee3@simplexi.com
 * @version 2.0
 */
class utilCacheManageFile extends utilCacheManageAbstract implements utilCacheManageInterface
{
    const CACHE_MANAGE_DIR = '_smng';
    const CACHE_MANAGE_EXT = '.dat';

    /**
     * 캐쉬 설정  - 파일 객체
     * @var utilCacheConfigFile
     */
    protected $cacheConfig;

    public function __construct(utilCacheConfig $cacgeConfig)
    {
        $this->cacheConfig = $cacgeConfig;
    }

    /**
     * 캐쉬 관리 설정
     *
     * @param    String    $sClassName
     * @param    String    $sKey
     *
     * @return    Mixed
     */
    public function setCacheManage($sClassName, $sKey)
    {
        $sCacheManageDir = $this->cacheConfig->sDefaultPath . DS . self::CACHE_MANAGE_DIR;

        $sCacheExpireFile = $sCacheManageDir . DS . $this->cacheConfig->sCacheName . DS . $sClassName . self::CACHE_MANAGE_EXT;

        if (!is_dir($sCacheManageDir . DS . $this->cacheConfig->sCacheName)) {
            utilFile::getInstance()->mkdir($sCacheManageDir . DS . $this->cacheConfig->sCacheName);
        }

        if (!is_file($sCacheExpireFile)) {
            file_put_contents($sCacheExpireFile, time());
        }

        return true;
    }

    /**
     * 캐쉬 만료여부 반환
     * (false 가 갱신한다는 의미)
     *
     * @param    String    $sClassName
     * @param    String    $sKey
     *
     * @return    Boolean
     */
    public function isCacheExpired($sClassName, $sKey)
    {
        $mCacheExpire = false;

        $sCacheManageDir = $this->cacheConfig->sDefaultPath . DS . self::CACHE_MANAGE_DIR;

        $sCacheExpireFile = $sCacheManageDir . DS . $this->cacheConfig->sCacheName . DS . $sClassName . self::CACHE_MANAGE_EXT;

        if (!is_file($sCacheExpireFile)) return false;

        $mCacheExpire = file_get_contents($sCacheExpireFile);

        if ($mCacheExpire==false)    return false;

        $sCacheDataFile = $this->getCacheDataFile($sClassName, $sKey);

        if (!is_file($sCacheDataFile)) {
            return false;
        }

        $iVaildTime = time() - $this->cacheConfig->iExpireTime;

        $iFileModifyTime = filemtime($sCacheDataFile);

        if (!is_numeric($mCacheExpire) || $mCacheExpire>$iFileModifyTime) {
            return false;
        }

        if (!is_numeric($iFileModifyTime) || $iFileModifyTime<$iVaildTime) {
            return false;
        }

        return true;
    }

    /**
     * 캐쉬 클래스 초기화
     *
     * @param    String    $sClassName
     *
     * @return Boolean
     */
    public function setClear($sClassName)
    {
        $sCacheClassExpireFile = $this->cacheConfig->sDefaultPath . DS . self::CACHE_MANAGE_DIR . DS
            . $this->cacheConfig->sCacheName . DS . $sClassName . self::CACHE_MANAGE_EXT;

        return unlink($sCacheClassExpireFile);
    }

    /**
     * 캐쉬 전체 초기화
     *
     * @return Boolean
     */
    public function setClearAll()
    {
        return utilFile::getInstance()->rmdir($this->cacheConfig->sDefaultPath . DS . self::CACHE_MANAGE_DIR . DS
            . $this->cacheConfig->sCacheName);
    }

    /**
     * 캐쉬 데이터 디렉토리 반환
     *
     * @param    String    $sClassName
     * @param    String    $sFileName
     *
     * @return    String
     */
    protected function getCacheDataDir($sClassName)
    {
        $sCacheName = $this->cacheConfig->sCacheName;

        return $this->cacheConfig->sDefaultPath . DS . utilCacheWriterFile::CACHE_DIR . DS
            . $sCacheName . DS . $sClassName;
    }

    /**
     * 캐쉬 데이터 파일경로 반환
     *
     * @param    String    $sClassName
     * @param    String    $sFileName
     *
     * @return    String
     */
    protected function getCacheDataFile($sClassName, $sFileName)
    {
        $iDirectoryVariance = $this->cacheConfig->iDirectoryVariance;

        $sCacheDataPath = $this->getCacheDataDir($sClassName);

        if (!empty($sFileName)) {
            if ($iDirectoryVariance>0) {
                $aDirectoryVariance = array();

                for ($i=0; $i<$iDirectoryVariance; $i++) {
                    $aDirectoryVariance[] = substr($sFileName, $i, 1);
                }

                $sCacheDataPath = $sCacheDataPath . DS . implode(DS, $aDirectoryVariance);
            }
        }

        return $sCacheDataPath . DS. $sFileName . utilCacheWriterFile::CACHE_EXT . '.' . strtolower($this->cacheConfig->sWriterParser);
    }
}